﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Frame.Lib.Core.User;
using Frame.Lib.Core.Role;
using Frame.Lib.Utilities;
using Frame.Lib.Core.Module;
using Frame.Lib.Common;
using Frame.Lib.Enums;
using Frame.Lib.Core.Tree;

public partial class Admin_User_SaveModule : PageSelectTree
{
    private ModuleBO module
    {
        get
        {
            return ModuleBO.Instance<ModuleBO>();
        }
    }

    IModuleAccess _access;
    IModuleAccess Access
    {
        get
        {
            if (_access == null)
            {
                _access = new UserBO();
            }
            return _access;
        }

    }

    RoleBO role
    {
        get
        {
            return RoleBO.Instance<RoleBO>();
        }
    }
    UserBO user
    {
        get
        {
            return UserBO.Instance<UserBO>();
        }
    }

    private int UserID
    {
        get
        {
            return int.Parse(StrUtil.DecryptUrl(Request["UserID"]));
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (!IsPostBack)
        {
            BindTree();           
            lblName.Text = user.GetName(UserID);
                    
        }
        tvModule.Attributes.Add("onclick", "CascadSelect('" + tvModule.ClientID + "')");
    }

    private void BindTree()
    {       

        IEnumerable<ModuleInfo> list = module.GetList();

        if (AppSession.UserID != Constants.ADMIN_ID)
        {
            IEnumerable<int> moduleIDs = user.GetModuleIDs(AppSession.UserID, role);
            list = list.Where(m => moduleIDs.Contains(m.ID));
        }

        list = list.Where(T => T.State == (int)ModuleState.Enable);

        List<int> lstModuleIDs = Access.GetModuleIDs(UserID);
        module.BindTreeView(tvModule, list, true, lstModuleIDs);

        List<int> lstRoleIDs = user.GetRoleIDs(UserID);
        List<int> lstRoleModuleIDs = role.GetModuleIDs(lstRoleIDs);
        SetDisable(tvModule.Nodes[0], lstRoleModuleIDs);
    }

    private void SetDisable(TreeNode treeNode, List<int> lstRoleModuleIDs)
    {
        
        int moduleID = int.Parse(treeNode.Value);
        if (lstRoleModuleIDs.Contains(moduleID))
        {
            treeNode.ShowCheckBox = false;
            TreeBO<TreeInfo>.ExpendNode(treeNode.Parent);

            Regex reg = new Regex(@"<.*/>");            

            treeNode.Text = reg.Replace(treeNode.Text,"<input type='checkbox' disabled='true' checked='true' />");
            
        }
        foreach (TreeNode node in treeNode.ChildNodes)
        {
            SetDisable(node, lstRoleModuleIDs);
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        List<int> lstModuleIDs = StrUtil.SplitToInt(CheckedIDs);

        Access.SaveModuleIDs(UserID, lstModuleIDs);
        string strScript = "window.close();returnValue=true";
        JSUtil.ResponseJS(strScript);
    }
    
}
